# online_answer_records/router.py
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from typing import List
from ..database import get_db
from . import schemas, crud

router = APIRouter(prefix="/online_answer_records", tags=["online_answer_records"])

@router.post("/", response_model=schemas.OnlineAnswerRecord, summary="添加在线问答记录", description="创建新的在线问答记录")
def create_online_answer_record(
    record: schemas.OnlineAnswerRecordCreate,
    db: Session = Depends(get_db)
):
    return crud.crud_online_answer_record.create(db, record)

@router.get("/", response_model=List[schemas.OnlineAnswerRecord], summary="获取所有在线问答记录", description="获取所有在线问答记录，支持分页")
def read_online_answer_records(
    skip: int = 0,
    limit: int = 100,
    db: Session = Depends(get_db)
):
    return crud.crud_online_answer_record.get_all(db, skip=skip, limit=limit)

@router.get("/{record_id}", response_model=schemas.OnlineAnswerRecord, summary="获取单个在线问答记录", description="根据记录ID获取在线问答记录详情")
def read_online_answer_record(
    record_id: int,
    db: Session = Depends(get_db)
):
    db_record = crud.crud_online_answer_record.get_by_id(db, record_id)
    if db_record is None:
        raise HTTPException(status_code=404, detail="Online answer record not found")
    return db_record

@router.get("/user/{user_id}", response_model=List[schemas.OnlineAnswerRecord], summary="获取用户的在线问答记录", description="根据用户ID获取所有在线问答记录")
def read_online_answer_records_by_user(
    user_id: int,
    skip: int = 0,
    limit: int = 100,
    db: Session = Depends(get_db)
):
    return crud.crud_online_answer_record.get_by_user(db, user_id, skip=skip, limit=limit)

@router.get("/session/{session_id}", response_model=List[schemas.OnlineAnswerRecord], summary="获取会话的在线问答记录", description="根据会话ID获取所有在线问答记录")
def read_online_answer_records_by_session(
    session_id: str,
    db: Session = Depends(get_db)
):
    return crud.crud_online_answer_record.get_by_session(db, session_id)

@router.put("/{record_id}", response_model=schemas.OnlineAnswerRecord, summary="更新在线问答记录", description="根据记录ID更新在线问答记录")
def update_online_answer_record(
    record_id: int,
    record: schemas.OnlineAnswerRecordUpdate,
    db: Session = Depends(get_db)
):
    db_record = crud.crud_online_answer_record.update(db, record_id, record)
    if db_record is None:
        raise HTTPException(status_code=404, detail="Online answer record not found")
    return db_record

@router.delete("/{record_id}", summary="删除在线问答记录", description="根据记录ID删除在线问答记录")
def delete_online_answer_record(
    record_id: int,
    db: Session = Depends(get_db)
):
    success = crud.crud_online_answer_record.delete(db, record_id)
    if not success:
        raise HTTPException(status_code=404, detail="Online answer record not found")
    return {"message": "Online answer record deleted successfully"}